package com.vincent.netty;

import sun.misc.Unsafe;
import sun.reflect.ReflectionFactory;

import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.security.AccessController;

/**
 * Description
 *
 * @author yangwenchang
 * @version 1.0
 * @date 2018-02-24
 */
public class TestMain {
    //private static final ReflectionFactory reflectionFactory = (ReflectionFactory) AccessController.doPrivileged(new ReflectionFactory.GetReflectionFactoryAction());

    public static void main(String[] args) throws Exception {
        //Constructor constr = reflectionFactory.newConstructorForSerialization(Test.class, Object.class.getConstructor(new Class[0]));
        //Object o =  constr.newInstance(new Object[0]);
        //System.out.println(Serializable.class.isAssignableFrom(o.getClass()));
        //Test test = (Test) o;
        //test.say();
        //

        //Test t = Test.class.newInstance();
        //System.out.println(t);

        //ReflectionFactory relfactory = ReflectionFactory.getReflectionFactory();
        //Constructor cons = relfactory.newConstructorForSerialization(Test.class, Object.class.getConstructor());
        //Test o = (Test) cons.newInstance();
        //System.out.println(o);

        Unsafe unsafe = getUnsafe();
        Test test = (Test) unsafe.allocateInstance(Test.class);
        System.out.println(test.toString());

    }

    public static Unsafe getUnsafe() {
        try {
            Field f = Unsafe.class.getDeclaredField("theUnsafe");
            f.setAccessible(true);
            return (Unsafe)f.get(null);
        } catch (Exception e) {
            return null;
        }
    }
}
